package org.dey.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.dey.entity.Result;
import org.dey.mapper.OrderMapper;
import org.dey.pojo.*;
import org.dey.service.OrderService;
import org.dey.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hg
 * @date 2022/3/15
 * @classname
 */
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderSettingService orderSettingService;

    @Override
    public PageVo<Order> yuYueLbXx(Order order,int pageNum,int pagesize) {

        PageHelper.startPage(pageNum, pagesize);

        List<Order> list = orderMapper.queryorder(order.getName());

        PageInfo<Order> pageInfo = new PageInfo<Order>(list);

        PageVo<Order> pageVo = new PageVo<Order>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return  pageVo;
    }

    @Override
    public Result zjYuYueLbXx(Order order) {

        Result result =null;


        String format = new SimpleDateFormat("yyyy-MM-dd").format(order.getCheckDate());

        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
        queryWrapper.eq("orderDate",format);

        OrderSetting one = orderSettingService.getOne(queryWrapper);


        if(one!=null){
            if(one.getNumber()>one.getReservations()){

                UpdateWrapper<OrderSetting> updateWrapper = new UpdateWrapper<OrderSetting>();
                updateWrapper.eq("orderDate",format).setSql("reservations = reservations+"+1);
                boolean update = orderSettingService.update(updateWrapper);



                if(update){
               //预约成功 预约设置表 已预约加一

                    order.setOrderDate(new Date());
                    order.setOrderType("医院预约");
                    boolean save = this.save(order);

                    if(save){
                        result = new Result(true,"新增预约成功");
                    }
                }
            }else{
                result = new Result(true,"该时间预约已满");
            }
        }else{
            result = new Result(true,"该时间不可预约");
        }




        return result;
    }

    @Override
    public int scYuYueLbXx(int id) {
        boolean save = this.removeById(id);
        return save==true?1:0;
    }

    @Override
    public int xgYuYueLbXx(Order order) {
        boolean update = this.updateById(order);
        return update==true?1:0;
    }

    @Override
    public Result qdyy(int id) {

        Result result =null;

        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<Order>();

        updateWrapper.eq("id",id).set("orderStatus","已确定");

        boolean update = this.update(updateWrapper);

        if(update){
            result = new Result(true, "预约成功");
        }else {
            result = new Result(true,"预约失败");
        }

        return result;
    }

    @Override
    public Result qxyy(int id) {
        Result result = null;

        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<Order>();

        updateWrapper.eq("id", id).set("orderStatus", "已取消");

        boolean update = this.update(updateWrapper);

        if (update) {
            result = new Result(true, "取消成功");
        } else {
            result = new Result(true, "取消失败");
        }

        return result;
    }
}
